/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import DTO.CKhoaSo;
import DTO.CFields;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CKhoaSoDAO extends IData<CKhoaSo>{
    public CKhoaSoDAO(){}

    @Override
    public CKhoaSo add(CKhoaSo item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void add(CKhoaSo item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int addrows(List<CKhoaSo> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CKhoaSo Find(CKhoaSo item, List<CKhoaSo> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(CKhoaSo item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object insertOnSubmit(CKhoaSo item, SubConfig config) {
        Object rs = 0;
        String mysql = "insert into khoa_so(thang,nam) values(?,?)";
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setInt(1, item.getThang());
            cs.setInt(2, item.getNam());
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                rs = (long) keyResultSet.getInt(CFields.id);     
            }
        } catch (SQLException ex) {
            Logger.getLogger(CKhoaSoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    public int Khoa_So(int thang_truoc,int nam_truoc,int thang,int nam, SubConfig config){
        String mysql = "insert into ton_kho(ten_san_pham,san_pham_id,kho_id,ton_toi_thieu,ton_toi_da,so_luong_nhap,so_luong_xuat,thang,nam,tondau) ";
        mysql = mysql + String.format(" select ten_san_pham,san_pham_id,kho_id,ton_toi_thieu,ton_toi_da,0,0,%s,%s,(tondau +so_luong_nhap - so_luong_xuat) as td ",
                thang,nam);
        mysql = mysql + String.format("from ton_kho where thang = ? and nam = ?");
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql);
            cs.setInt(1, thang_truoc);
            cs.setInt(2, nam_truoc);
          int t =  cs.executeUpdate();
          return t;
        } catch (SQLException ex) {
            Logger.getLogger(CKhoaSoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return -1;
    }
    
    @Override
    public Object updateOnSubmint(CKhoaSo item, SubConfig config) {
        Object rs = 0;
        String mysql = "update khoa_so set khoa_so=true where id= ?";
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql);
            cs.setLong(1, item.getId());
            rs = cs.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(CKhoaSoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return rs;
    }

    @Override
    public Object deleteOnSubmint(CKhoaSo item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CKhoaSo getItem(ResultSet rs) {
        CKhoaSo item = new CKhoaSo();
        try {
            item.setId(rs.getLong(CFields.id));
            item.setNam(rs.getInt(CFields.nam));
            item.setThang(rs.getInt(CFields.thang));
            item.setKhoa_so(rs.getBoolean("khoa_so"));
        } catch (SQLException ex) {
            Logger.getLogger(CKhoaSoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public List<CKhoaSo> getAll(SubConfig config) {
        String mysql = "select * from khoa_so order by id desc";
        return this.getAll(mysql, config);
    }
}
